package vincent.com.layouttest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by zhengwenchen on 2016/3/16.
 * only works in android-19 and higher
 */
public class CircleClipImageView extends ImageView {
    private int mRadius;
    public CircleClipImageView(Context context) {
        this(context, null);
    }

    public CircleClipImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleClipImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mRadius = (int)(40 * context.getResources().getDisplayMetrics().density);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path circlePath = new Path();
        int x = getMeasuredWidth()/2;
        int y = getMeasuredHeight()/2;
        circlePath.addCircle(x, y, mRadius, Path.Direction.CCW);

        canvas.clipPath(circlePath);
        super.onDraw(canvas);

    }
}
